﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOS_COMMON_SRC_OBJECTS_ID_GENERATOR_H__
#define __XOS_COMMON_SRC_OBJECTS_ID_GENERATOR_H__

#include "../tools/head.h"
#include "id.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace xos_common
{

	// 
	// id生成器，考虑已有id,生成顺序的不重复id.可以在四字节数值中轮转(去除了-1, 0, 1三个值)
	// 

	class id_generator : public xos_stl::mem_item< xos::release_impl< id_generator >, thread_lock >,
		public i_id_generator
	{
	public:
		typedef xos_stl::xos_list< id > ID_LIST;
		typedef ID_LIST::iterator ID_ITER;
        typedef xos::release_impl< id_generator > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_addr( POOL *** pppPool, LIST *** pppList );

    public:
		id_generator();
		~id_generator();

	public:
		int init();
		int term();

	protected:
		xos_set::sets< id, int, int > id_set;
        ID_LIST ids;
		int start;

	public:
		int remove( int mid );
		int add( int mid );
		int gen( int * lpnId );
	};
}

#endif // __XOS_COMMON_SRC_OBJECTS_ID_GENERATOR_H__
